package org.lex.ftp.ui.find.filter;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import org.lex.filter.Filter;
import org.lex.filter.KeywordFilter;
import org.lex.filter.KeywordFilterPane;

public class GeneralPane extends JPanel implements Filter<String> {
	private JPanel locationPane = null;
	private JLabel hostLabel = null;
	private JTextField hostTextField = null;
	private JLabel pathLabel = null;
	private JTextField pathTextField = null;
	private KeywordFilterPane keywordPane = null;
	private JPanel progressPane = null;
	private JLabel progressLabel = null;
	private JProgressBar progressBar = null;

	public GeneralPane() {
		super();
		initialize();
	}

	public boolean accept(String str) {
		return this.getKeywordPane().getKeywordFilter().accept(str);
	}

	/*
	 * location pane's access method
	 */
	public void setHost(String host) {
		this.getHostTextField().setText(host);
	}

	public String getHost() {
		return this.getHostTextField().getText().trim();
	}

	public void setPath(String path) {
		this.getPathTextField().setText(path);
	}

	public String getPath(String path) {
		return this.getPathTextField().getText().trim();
	}

	/*
	 * keyword pane's access method
	 */
	public KeywordFilter getKeywordFilter() {
		return this.getKeywordPane().getKeywordFilter();
	}

	public KeywordFilter getKeywordFilter(KeywordFilter query) {
		return this.getKeywordPane().getKeywordQuery(query);
	}

	public JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setStringPainted(true);
		}
		return progressBar;
	}

	// =======================
	private void initialize() {
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 0;
		gridBagConstraints3.fill = GridBagConstraints.BOTH;
		gridBagConstraints3.weightx = 1.0;
		gridBagConstraints3.weighty = 1.0;
		gridBagConstraints3.gridy = 3;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.weightx = 1.0;
		gridBagConstraints2.insets = new Insets(0, 8, 5, 7);
		gridBagConstraints2.gridy = 2;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.insets = new Insets(0, 6, 5, 5);
		gridBagConstraints1.gridy = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.insets = new Insets(5, 6, 3, 5);
		gridBagConstraints.weightx = 1.0;
		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());
		this.add(getLocationPane(), gridBagConstraints);
		this.add(getKeywordPane(), gridBagConstraints1);
		this.add(getProgressPane(), gridBagConstraints2);
		this.add(Box.createVerticalGlue(), gridBagConstraints3);
	}

	public JPanel getLocationPane() {
		if (locationPane == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.insets = new Insets(0, 6, 5, 5);
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.gridy = 1;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 1;
			gridBagConstraints7.insets = new Insets(0, 6, 5, 0);
			pathLabel = new JLabel();
			pathLabel.setText("Path");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 6, 5, 5);
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(0, 6, 5, 0);
			hostLabel = new JLabel();
			hostLabel.setText("Host");
			locationPane = new JPanel();
			locationPane.setLayout(new GridBagLayout());
			locationPane.setBorder(BorderFactory.createTitledBorder(null, "Location", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			locationPane.add(hostLabel, gridBagConstraints9);
			locationPane.add(getHostTextField(), gridBagConstraints6);
			locationPane.add(pathLabel, gridBagConstraints7);
			locationPane.add(getPathTextField(), gridBagConstraints8);
		}
		return locationPane;
	}

	private JTextField getHostTextField() {
		if (hostTextField == null) {
			hostTextField = new JTextField();
			hostTextField.setEditable(false);
		}
		return hostTextField;
	}

	private JTextField getPathTextField() {
		if (pathTextField == null) {
			pathTextField = new JTextField();
		}
		return pathTextField;
	}

	private KeywordFilterPane getKeywordPane() {
		if (keywordPane == null) {
			keywordPane = new KeywordFilterPane();
			keywordPane.setBorder(BorderFactory.createTitledBorder(null, "Keyword", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		}
		return keywordPane;
	}

	private JPanel getProgressPane() {
		if (progressPane == null) {
			progressLabel = new JLabel();
			progressLabel.setText("Progress");
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(6);
			borderLayout.setVgap(6);
			progressPane = new JPanel();
			progressPane.setLayout(borderLayout);
			progressPane.add(progressLabel, java.awt.BorderLayout.WEST);
			progressPane.add(getProgressBar(), java.awt.BorderLayout.CENTER);
		}
		return progressPane;
	}
}
